package com.gsmr.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.SocketUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.GPRS_default;
import com.gsmr.entity.GPRS_full;
import com.gsmr.entity.PageBean;
import com.gsmr.service.GPRS_defaultService;
import com.gsmr.service.GPRS_fullService;
import com.gsmr.util.DateUtil;
import com.gsmr.util.ExcelUtil;
import com.gsmr.util.FormatDate;
import com.gsmr.util.ResponseUtil;
import com.gsmr.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 作者 : 黄志新
 * @version 创建时间：2017年8月11日 上午12:13:57 类说明：GPRS_fullController层
 */
@Controller
@RequestMapping("/gprs_full")
public class GPRS_fullController {

	@Resource
	private GPRS_fullService gprs_fullService;

	List<GPRS_full> gprs_fullList;

	/**
	 * 分页条件查询GPRS_full
	 */
	@RequestMapping("/list")
	public void list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, 
			@RequestParam(value = "startTime", required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime, 
		@RequestParam(value = "IMSI", required = false) String IMSI, @RequestParam(value = "MSISDN", required = false) String MSISDN, 
		@RequestParam(value = "gEngineNo", required = false) String gEngineNo, @RequestParam(value = "gTrainNo", required = false) String gTrainNo, 
		@RequestParam(value = "MessageType", required = false) String MessageType, @RequestParam(value = "TLLI", required = false) String TLLI, 
		@RequestParam(value = "search", required = false) String search, @RequestParam(value = "increase", required = false) String increase, 
		@RequestParam(value = "destIP", required = false) String destIP, @RequestParam(value = "sourceIP", required = false) String sourceIP,
		@RequestParam(value = "gServiceType", required = false) String gServiceType, @RequestParam(value = "recordId", required = false) String recordId, 
		@RequestParam(value = "BSName", required = false) String BSName, HttpServletResponse response) throws Exception {

		if (search == null) return;
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<>();
		map.put("table_name", "gprs_full");
		map.put("IMSI", IMSI);
		map.put("MSISDN", MSISDN);
		map.put("gEngineNo", gEngineNo);
		map.put("gTrainNo", gTrainNo);
		map.put("MessageTypeAll", MessageType);
		map.put("TLLI", TLLI);
		map.put("start", pageBean.getStart());
		map.put("destIP", destIP);
		map.put("sourceIP", sourceIP);
		map.put("recordId", recordId);
		
		if (StringUtil.isNotEmpty(BSName)) {
			String[] subName = BSName.split(",");
			map.put("BSNames", Arrays.asList(subName));
		}
		
		
		if (gServiceType != null && gServiceType.length() > 0) {
			List<String> serviceType = Arrays.asList(gServiceType.split(","));
			if (serviceType != null && serviceType.size() > 0) {
				map.put("serviceType", serviceType);
			} else {
				map.put("serviceType", null);
			}
		} else {
			map.put("serviceType", null);
		}

		if (MessageType != null && !MessageType.equals("")) {
			String[] subUmType = MessageType.split(",");
			List<String> subList = Arrays.asList(subUmType);
			map.put("MessageType", subList);
		} else {
			map.put("MessageType", "");
		}
		//此处按照时间获取对应天表
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyyMMdd"));
			System.out.println("startime格式处理："+map.get("startTime"));
		}
		if (endTime != null && !endTime.equals("NaN")) {
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyyMMdd"));
			System.out.println("endtime格式处理："+map.get("endTime"));
		}

		List<String> table_names = gprs_fullService.findTableNames(map);
		//此处将前端传送的String格式数据（时间戳类型），转换成数据库可比较格式
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyy-MM-dd HH:mm:ss"));

			System.out.println("格式处理："+map.get("startTime"));
		}
		if (endTime != null && !endTime.equals("NaN")) {
//			map.put("endTime", endTime);
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyy-MM-dd HH:mm:ss"));
			System.out.println("格式处理："+map.get("endTime"));
		}
		
		gprs_fullList = new ArrayList<>();
		if (table_names != null && table_names.size() > 0) {
			map.put("start", 0);
			map.put("size", pageBean.getPageSize());
			if (increase.equals("1")) {
				for (int i = table_names.size() - 1; i >= 0 && gprs_fullList.size() < pageBean.getPageSize(); i--) {

					map.put("table_name", table_names.get(i));
					List<GPRS_full> sub_gprs_fullList = gprs_fullService.findASC(map);
					gprs_fullList.addAll(sub_gprs_fullList);
				}
			}
			else {
				for (int i = 0; i < table_names.size() && gprs_fullList.size() < pageBean.getPageSize(); i++) {
					map.put("table_name", table_names.get(i));
					List<GPRS_full> sub_gprs_fullList = gprs_fullService.find(map);
					gprs_fullList.addAll(sub_gprs_fullList);
				}
			}
		}
		System.out.println(gprs_fullList.size());
		
		for(GPRS_full g : gprs_fullList) {
			if(g.getgInfoName() == 129) {
				g.setgOrder(81);
			}
			else if(g.getgInfoName() == 130) {
				g.setgOrder(82);
			}
			if(g.getgOrder()== 81 || g.getgOrder()==83){
				if(g.getgData().contains("(81H)")) {
					g.setgOrder(51);
					System.out.println("调度命令自动确认");
				}
				if(g.getgData().contains("(82H)")) {
					g.setgOrder(53);
					System.out.println("调度命令手动确认");
				}

			}
		}
		
		if (!search.equals("3")) {
		GPRS_fullController.ListSort(gprs_fullList);
		}
		if (gprs_fullList.size() > pageBean.getPageSize() && increase.equals("1")) {
			gprs_fullList = gprs_fullList.subList(0, pageBean.getPageSize());
		} else if (gprs_fullList.size() > pageBean.getPageSize() && increase.equals("0")) {
			gprs_fullList = gprs_fullList.subList(gprs_fullList.size() - pageBean.getPageSize(), gprs_fullList.size());
		}

		/*
		 * 避免返回的第一页不够100条
		 */
		if (gprs_fullList.size() < pageBean.getPageSize() && increase.equals("0") && gprs_fullList.size() > 0) {
			long tmptime1 = gprs_fullList.get(0).getUnixTimeStamp();
			map.put("endTime", null);
			map.put("startTime", DateUtil.longToString(tmptime1, "yyyyMMdd"));
			table_names = gprs_fullService.findTableNames(map);
			map.put("endTime", tmptime1);
			gprs_fullList.clear();
			if (table_names != null) {
				map.put("start", 0);
				map.put("size", pageBean.getPageSize());
				for (int i = table_names.size() - 1; i >= 0 && gprs_fullList.size() < pageBean.getPageSize(); i--) {
					map.put("table_name", table_names.get(i));
					List<GPRS_full> sub_gprs_fullList = gprs_fullService.findASC(map);
					gprs_fullList.addAll(sub_gprs_fullList);
				}
			}
			if (!search.equals("3")) {
			GPRS_fullController.ListSort(gprs_fullList);
			}
			if (gprs_fullList.size() > pageBean.getPageSize()) {
				gprs_fullList = gprs_fullList.subList(0, pageBean.getPageSize());
			}
		}

		JSONObject result = new JSONObject();
		setupData(gprs_fullList);
		JSONArray jsonArray = JSONArray.fromObject(gprs_fullList);
		result.put("rows", jsonArray);
		result.put("total", Integer.MAX_VALUE);
		ResponseUtil.write(response, result);
	}

	private void setupData(List<GPRS_full> gprs_fullList) {
		for (GPRS_full o : gprs_fullList) {
			if (o.getTLLI() != null && o.getTLLI().length() > 0) {
				try {
					Long a = Long.valueOf(o.getTLLI());
					o.setTLLI(Long.toHexString(a).toString().toUpperCase());
				} catch (Exception e) {

				}
			}
			if (o.getDirection() != null && o.getDirection().length() > 0) {
				o.setDirection(o.getDirection().equals("UPLINK") ? "上行" : "下行");
			}
		}
	}
	

	@Resource
	private GPRS_defaultService gprs_defaultService;
	
	@RequestMapping("/findTLLI")
	public void findTLLI(@RequestParam(value = "IMSI", required = false) Integer IMSI, @RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime, HttpServletResponse response) throws Exception {
		
		Map<String, Object>map = new HashMap<>();
		//map.put("table_name", "gprs_full_20180924");
		map.put("IMSI", IMSI);
		//List<Integer> l = gprs_fullService.findTLLI(map);
		
		
		
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyyMMdd"));
		}
		if (endTime != null && !endTime.equals("NaN")) {
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyyMMdd"));
		}

		List<String> table_names = gprs_fullService.findTableNames(map);
		
		if (startTime != null && !startTime.equals("NaN")) {
//			map.put("startTime", startTime);
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyy-MM-dd HH:mm:ss"));
		}

		if (endTime != null && !endTime.equals("NaN")) {
//			map.put("endTime", endTime);
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyy-MM-dd HH:mm:ss"));

		}
		
		List<Integer> TLLIs = new ArrayList<>();
		for (String tableName: table_names) {
			map.put("table_name", tableName);
			
			List<Integer> l = gprs_fullService.findTLLI(map);
			TLLIs.addAll(l);
		}
		
		
		
//		map = new HashMap<>();
//		map.put("TLLI", l);
//		map.put("table_name", "gprs_full_20180924");
//		List<GPRS_default> GL = gprs_defaultService.findInTLLI(map);
		
		JSONObject result = new JSONObject();
		result.put("messageContent", TLLIs);
		ResponseUtil.write(response, result);
	}
	

	/**
	 * 查找IMSI列表
	 */
	@RequestMapping("/gprs_fullIMSIComboList")
	public void gprs_fullIMSIComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "gprs_full");
		List<GPRS_full> gprs_fullList = gprs_fullService.findIMSIs(map);
		JSONArray rows = JSONArray.fromObject(gprs_fullList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找MSISDN列表
	 */
	@RequestMapping("/gprs_fullMSISDNComboList")
	public void gprs_fullMSISDNComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "gprs_full");
		List<GPRS_full> gprs_fullList = gprs_fullService.findMSISDNs(map);
		JSONArray rows = JSONArray.fromObject(gprs_fullList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找Message_TYPE列表
	 */
	@RequestMapping("/gprs_fullMessage_TYPEComboList")
	public void gprs_fullMessage_TYPEComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "gprs_full");
		List<GPRS_full> gprs_fullList = gprs_fullService.findMessage_TYPEs(map);
		JSONArray rows = JSONArray.fromObject(gprs_fullList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找机车号列表
	 */
	@RequestMapping("/gprs_fullGEngineNoComboList")
	public void gprs_fullGEngineNoComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "gprs_full");
		List<GPRS_full> gprs_fullList = gprs_fullService.findGEngineNos(map);
		JSONArray rows = JSONArray.fromObject(gprs_fullList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找车次号列表
	 */
	@RequestMapping("/gprs_fullGTrainNoComboList")
	public void gprs_fullGTrainNoComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "gprs_full");
		List<GPRS_full> gprs_fullList = gprs_fullService.findGTrainNos(map);
		JSONArray rows = JSONArray.fromObject(gprs_fullList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}

	private List<String> findTableNames(String startTime, String endTime, String gServiceType, String MessageType, Map<String, Object> map) {

		if (gServiceType.length() > 0) {
			List<String> serviceType = Arrays.asList(gServiceType.split(","));
			if (serviceType != null && serviceType.size() > 0) {
				map.put("serviceType", serviceType);
			} else {
				map.put("serviceType", null);
			}
		} else {
			map.put("serviceType", null);
		}
		if (MessageType != null && !MessageType.equals("")) {
			String[] subUmType = MessageType.split(",");
			List<String> subList = Arrays.asList(subUmType);
			map.put("MessageType", subList);
		} else {
			map.put("MessageType", "");
		}
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyyMMdd"));
		}
		if (endTime != null && !endTime.equals("NaN")) {
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyyMMdd"));
		}
		List<String> table_names = gprs_fullService.findTableNames(map);
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", startTime);
		}
		if (endTime != null && !endTime.equals("NaN")) {
			map.put("endTime", endTime);
		}
		return table_names;
	}

	private String headers[] = { "触发时间 ", "IMSI", "MSISDN", "TLLI", "车次号", "机车号", "小区名称", "BCCH", "TCH", "时隙号","USF","TFI", "帧号", "方向", "Um接口消息类型", "DataLayer",
			"源IP地址", "目的IP地址", "源IP地址（包）", "目的IP地址（包 ）", "业务类型", "功能码", "解码信息", "调度命令", "数据长度 ", "LEVEL", "SNR", "QUALITY ","数据源码", "LAC", "CI", "前置机号", "记录号" };

	/**
	 * 导出gprs_full
	 */
	@RequestMapping("/export")
	public void export(@RequestParam(value = "startTime", required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime, @RequestParam(value = "IMSI", required = false) String IMSI, 
		@RequestParam(value = "MSISDN", required = false) String MSISDN, @RequestParam(value = "gEngineNo", required = false) String gEngineNo, @RequestParam(value = "gTrainNo", required = false) String gTrainNo,
		@RequestParam(value = "MessageType", required = false) String MessageType, @RequestParam(value = "TLLI", required = false) String TLLI, 
		@RequestParam(value = "BSName", required = false) String BSName, @RequestParam(value = "gServiceType", required = false) String gServiceType, 
		@RequestParam(value = "destIP", required = false) String destIP, @RequestParam(value = "sourceIP", required = false) String sourceIP,
		@RequestParam(value = "recordId", required = false) String recordId, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("table_name", "gprs_full");
		map.put("IMSI", IMSI);
		map.put("MSISDN", MSISDN);
		map.put("gEngineNo", gEngineNo);
		map.put("gTrainNo", gTrainNo);
		map.put("MessageTypeAll", MessageType);
		map.put("TLLI", TLLI);
		map.put("destIP", destIP);
		map.put("sourceIP", sourceIP);
		map.put("recordId", recordId);
		if (StringUtil.isNotEmpty(BSName)) {
			String[] subName = BSName.split(",");
			map.put("BSNames", Arrays.asList(subName));
		}

		//List<String> table_names = findTableNames(startTime, endTime, gServiceType, MessageType, map);
		
		if (gServiceType != null && gServiceType.length() > 0) {
			List<String> serviceType = Arrays.asList(gServiceType.split(","));
			if (serviceType != null && serviceType.size() > 0) {
				map.put("serviceType", serviceType);
			} else {
				map.put("serviceType", null);
			}
		} else {
			map.put("serviceType", null);
		}

		if (MessageType != null && !MessageType.equals("")) {
			String[] subUmType = MessageType.split(",");
			List<String> subList = Arrays.asList(subUmType);
			map.put("MessageType", subList);
		} else {
			map.put("MessageType", "");
		}
		if (startTime != null && !startTime.equals("NaN")) {
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyyMMdd"));
		}
		if (endTime != null && !endTime.equals("NaN")) {
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyyMMdd"));
		}

		List<String> table_names = gprs_fullService.findTableNames(map);
		if (startTime != null && !startTime.equals("NaN")) {
//			map.put("startTime", startTime);
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime), "yyyy-MM-dd HH:mm:ss"));
		}

		if (endTime != null && !endTime.equals("NaN")) {
//			map.put("endTime", endTime);
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime), "yyyy-MM-dd HH:mm:ss"));
		}

		
		List<GPRS_full> gprs_fullList_all = new ArrayList<>();
		List<GPRS_full> sub_gprs_fullList = new ArrayList<>();
		map.put("start", 0);

		if (table_names != null && table_names.size() > 0) {
			for (int i = table_names.size() - 1; i >= 0 && gprs_fullList_all.size() < ExcelUtil.maxExportRows; i--) {
				map.put("size", ExcelUtil.maxExportRows - gprs_fullList_all.size());
				sub_gprs_fullList.clear();
				map.put("table_name", table_names.get(i));
				sub_gprs_fullList = gprs_fullService.findASC(map);
				if (sub_gprs_fullList != null && sub_gprs_fullList.size() > 0) {
					gprs_fullList_all.addAll(sub_gprs_fullList);
				}
			}
		}
		System.out.println(gprs_fullList_all.size());
		
		for(GPRS_full g : gprs_fullList_all) {
			if(g.getgInfoName() == 129) {
				g.setgOrder(81);
			}
			else if(g.getgInfoName() == 130) {
				g.setgOrder(82);
			}
			if(g.getgOrder()== 81 || g.getgOrder()==83){
               if(g.getgData().contains("(81H)")) {
				   g.setgOrder(51);
				   System.out.println("调度命令自动确认");
			   }
				if(g.getgData().contains("(82H)")) {
					g.setgOrder(53);
					System.out.println("调度命令手动确认");
				}

			}
		}
		
		GPRS_fullController.ListSort(gprs_fullList_all);

		List<GPRS_full> sub_gprs_fullList_all = gprs_fullList_all;
		if (gprs_fullList_all.size() > ExcelUtil.maxExportRows) {
			sub_gprs_fullList_all = gprs_fullList_all.subList(0, ExcelUtil.maxExportRows);
		}
		Workbook wb = new HSSFWorkbook();
		setupData(sub_gprs_fullList_all);
		ExcelUtil.fillExcelData_GPRS_full(sub_gprs_fullList_all, wb, headers);
		ResponseUtil.export(response, wb, "GMM/SM/业务" + FormatDate.formatDate() + ".xls");
	}

	/**
	 * 导出当前页的数据
	 */
	@RequestMapping("/exportcurrentpage")
	public void exportcurrentpage(@RequestParam(value = "data") String data, HttpServletResponse response) throws Exception {

		if (data == null || data.length() < 3) return;

		List<GPRS_full> gprsfullList_all = new ArrayList<GPRS_full>();
		JSONArray jsonArray = JSONArray.fromObject(data);
		for (int i = 0; i < jsonArray.size(); i++) {
			GPRS_full gprsfull = new GPRS_full();

			gprsfull.setFrameNumber(jsonArray.getJSONObject(i).getInt("frameNumber"));
			gprsfull.setFileNo(jsonArray.getJSONObject(i).getInt("fileNo"));
			gprsfull.setStartTime(jsonArray.getJSONObject(i).getString("startTime"));
			gprsfull.setIMSI(jsonArray.getJSONObject(i).getString("IMSI"));
			gprsfull.setTLLI(jsonArray.getJSONObject(i).getString("TLLI"));
			gprsfull.setMSISDN(jsonArray.getJSONObject(i).getString("MSISDN"));
			gprsfull.setFrontProcessorID(Integer.valueOf(jsonArray.getJSONObject(i).getString("frontProcessorID")));
			gprsfull.setLAC(Integer.valueOf(jsonArray.getJSONObject(i).getString("LAC")));
			gprsfull.setCI(Integer.valueOf(jsonArray.getJSONObject(i).getString("CI")));
			gprsfull.setBSName(jsonArray.getJSONObject(i).getString("BSName"));
			gprsfull.setBCCH(Integer.valueOf(jsonArray.getJSONObject(i).getString("BCCH")));
			gprsfull.setTCH(Integer.valueOf(jsonArray.getJSONObject(i).getString("TCH")));
			gprsfull.setTimeSlot(jsonArray.getJSONObject(i).getString("timeSlot"));
			gprsfull.setUSF(jsonArray.getJSONObject(i).getString("USF"));
			gprsfull.setTFI(jsonArray.getJSONObject(i).getString("TFI"));
			gprsfull.setDirection(jsonArray.getJSONObject(i).getString("direction"));
			gprsfull.setMessageType(jsonArray.getJSONObject(i).getString("messageType"));
			gprsfull.setDataLayer(jsonArray.getJSONObject(i).getString("dataLayer"));
			gprsfull.setSourceIP(jsonArray.getJSONObject(i).getString("sourceIP"));
			gprsfull.setDestinationIP(jsonArray.getJSONObject(i).getString("destinationIP"));
			gprsfull.setSourceCode(jsonArray.getJSONObject(i).getString("sourceCode"));
			gprsfull.setDataLength(Integer.valueOf(jsonArray.getJSONObject(i).getString("dataLength")));
			gprsfull.setLEVEL(jsonArray.getJSONObject(i).getString("LEVEL"));
			gprsfull.setSNR(jsonArray.getJSONObject(i).getString("SNR"));
			gprsfull.setQUALITY(jsonArray.getJSONObject(i).getString("QUALITY"));
			gprsfull.setgSourceIP(jsonArray.getJSONObject(i).getString("gSourceIP"));
			gprsfull.setgDstIP(jsonArray.getJSONObject(i).getString("gDstIP"));
			gprsfull.setgServiceType(Integer.valueOf(jsonArray.getJSONObject(i).getString("gServiceType")));
			gprsfull.setgOrder(Integer.valueOf(jsonArray.getJSONObject(i).getString("gOrder")));
			//String gData = jsonArray.getJSONObject(i).getString("gData");
			gprsfull.setDecoded(jsonArray.getJSONObject(i).getString("decoded"));
			gprsfull.setgData(jsonArray.getJSONObject(i).getString("gData"));
			gprsfull.setgEngineNo(jsonArray.getJSONObject(i).getString("gEngineNo"));
			gprsfull.setgTrainNo(jsonArray.getJSONObject(i).getString("gTrainNo"));
			gprsfull.setgDispatchCode(jsonArray.getJSONObject(i).getString("gDispatchCode"));
			gprsfullList_all.add(gprsfull);
		}
		Workbook wb = new HSSFWorkbook();
		ExcelUtil.fillExcelData_GPRS_full(gprsfullList_all, wb, headers);
		ResponseUtil.export(response, wb, "GMM/SM/业务" + FormatDate.formatDate() + ".xls");
	}

	/**
	 * 用模板导出用户
	 */
	@RequestMapping("/exportByTemplate")
	public void exportByTemplate(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<GPRS_full> gprs_fullList = new ArrayList<GPRS_full>();
		if (ids != null && !ids.equals("null") && !ids.equals("")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			map.put("ids", idsList);
			List<String> table_names = gprs_fullService.findTableNames(map);
			if (table_names != null) {
				List<GPRS_full> sub_gprs_fullList;
				for (int i = 0; i < table_names.size(); i++) {
					map.put("table_name", table_names.get(i));
					sub_gprs_fullList = gprs_fullService.findByIds(map);
					if (sub_gprs_fullList != null && sub_gprs_fullList.size() > 0) {
						gprs_fullList.addAll(sub_gprs_fullList);
					}
				}
			}
			GPRS_fullController.ListSort(gprs_fullList);
			Workbook wb = ExcelUtil.fillExcelDataWithTemplate_GPRS_full(gprs_fullList, "gprs_fullExportTemplate.xls");
			ResponseUtil.export(response, wb, "利用模版导出excel" + FormatDate.formatDate() + ".xls");
		} else {
			List<GPRS_full> sub_gprs_fullList = null;
			List<String> table_names = gprs_fullService.findTableNames(map);
			if (table_names != null && table_names.size() > 0) {
				for (int i = 0; i < table_names.size(); i++) {
					sub_gprs_fullList = null;
					map.put("table_name", table_names.get(i));
					sub_gprs_fullList = gprs_fullService.find(map);
					if (sub_gprs_fullList != null && sub_gprs_fullList.size() > 0) {
						gprs_fullList.addAll(sub_gprs_fullList);
					}
				}
			}
			GPRS_fullController.ListSort(gprs_fullList);
			Workbook wb = ExcelUtil.fillExcelDataWithTemplate_GPRS_full(gprs_fullList, "gprs_fullExportTemplate.xls");
			ResponseUtil.export(response, wb, "利用模版导出excel" + FormatDate.formatDate() + ".xls");
		}
	}

	/**
	 * 对List中每个对象元素逆序排序
	 */
	private static void ListSort(List<GPRS_full> list) {
		if (list == null || list.size() < 2) return;
		Collections.sort(list, new Comparator<GPRS_full>() {
			@Override
			public int compare(GPRS_full sf1, GPRS_full sf2) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
				try {
					Date dt1 = format.parse(sf1.getStartTime());
					Date dt2 = format.parse(sf2.getStartTime());
					if (dt1.getTime() < dt2.getTime()) {
						return -1;
					} else if (dt1.getTime() > dt2.getTime()) {
						return 1;
					} else {
						return 0;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 0;
			}
		});
	}
}